package tsb.trinhnx.tree;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileImplementation extends FileTree {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4311484700925689830L;
	private static final int DEFAULT_NUM_FOLDER = 2;
	private static final int DEFAULT_NUM_FILE = 20;
	/**
	 * 
	 */
	private File root;
	private List<File> subFolders;
	private List<File> subFiles;

	private FileImplementation() {
	} // private constructor

	/**
	 * Generation with path
	 * @param path
	 */
	public FileImplementation(final String path) {
		this(new File(path));
	}

	public FileImplementation(final File root) {
		this.root= root;
		subFiles = new ArrayList<>(DEFAULT_NUM_FILE);
		subFolders = new ArrayList<>(DEFAULT_NUM_FOLDER);

		List<File> children = getChildren(root);
		for (File file : children) {
			if (file.isFile())
				subFiles.add(file);
			else
				subFolders.add(file);
		}

	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		dumpNodeStructure(builder, root, "- ");
		return builder.toString();
	}

	private void dumpNodeStructure(StringBuilder builder, File node, String prefix) {
		if (node != null) {
			builder.append(prefix);
			builder.append(node.toString());
			builder.append('\n');
			prefix = "    " + prefix;
		}
		for (File child : getChildren(node)) {
			dumpNodeStructure(builder, child, prefix);
		}
	}

	public static void main(String[] args) {
		FileImplementation test = new FileImplementation("C:\\Web");
		System.out.println(test);
	}
}
